<template>
  <div class="login-main">
    <div class="login-container">
      <h2>注册</h2>
      <el-input v-model="registerForm.name" placeholder="用户名"></el-input>
      <el-input v-model="registerForm.password" placeholder="密码" type="password"></el-input>
      <el-button type="success" @click="register">注册</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import request from "@/utils/request";
import { ElMessage } from "element-plus";

const registerForm = ref({
  name: '',
  password: ''
});

const router = useRouter();

const register = () => {
  request.post('/sign', registerForm.value).then(response => {
    if (response.code === 200) {
      ElMessage.success("注册成功");
      router.push('/');
    } else {
      ElMessage.error(response.message);
    }
  }).catch(error => {
    console.error('注册请求错误:', error);
    ElMessage.error("注册请求错误");
  });
};
</script>

<style scoped>
.login-main {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}
.login-container {
  width: 300px;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
</style>
